from rest_framework import serializers
from django.contrib.auth.hashers import make_password
from apps.users.models import User


class UserModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ["id", "username", "mobile", "email", "password"]

        extra_kwargs = {
            "password": {"write_only": True, "required": True},
            "username": {"required": True},
            "mobile": {"required": True},
            "email": {"required": True}
        }


    def validate(self, attrs):
        password = attrs.pop("password")
        password = make_password(password)
        attrs['password'] = password
        attrs['is_staff'] = True
        return attrs





    # def create(self, validated_data):
    #     # 原来create方法默认是false
    #     validated_data["is_staff"] = True
    #     return User.objects.create_user(**validated_data)





